home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / mint / utils / stcron3.lzh / MISC.C < prev    next >
C/C++ Source or Header  |  1993-10-03  |  2KB  |  111 lines

  1. /* CROND & CRONTAB: (c) Kees Lemmens, Netherlands; June 1993.
  2.  
  3.     Programs for ATARI ST (running under MINT) to make it possible
  4.     to run background jobs at regular intervals.
  5.   
  6.     Any questions or suggestions about this program can be send to:
  7.     lemmens@dv.twi.tudelft.nl
  8. */
  9.  
  10. #include "cron.h"
  11. #include <string.h>
  12. #include <stdlib.h>
  13. #include <time.h>
  14. #include <signal.h>
  15. #include <errno.h>
  16. #include <stdarg.h>
  17.  
  18. char *ux2dos(char *string)    /* convert / to \ */
  19. {    char *tmp;
  20.     while((tmp=strchr(string,'/')) != NULL)
  21.         *tmp='\\';
  22.     return string;
  23. }
  24.  
  25. void LogMsg(char *name,int pid,char *fmt,...)
  26. {
  27.   /* If LOGFILE is defined this function writes to the */
  28.   /* appropriate file; otherwise, it does nothing. */
  29.  
  30. #if defined(LOGFILE) || defined(DEBUG)
  31.     va_list ptr;
  32.     char *msg;
  33.     long now = time((long *) 0);
  34.     char *ti = ctime(&now);
  35.     FILE *log_fd;
  36.  
  37.     msg = malloc(strlen(name) + strlen(fmt) + MAX_TEMPSTR);
  38.     va_start(ptr,fmt);
  39.  
  40.     sprintf(msg, "%s (%.3d) %.24s: %s\n", name, pid, ti, fmt);
  41.  
  42. #ifdef DEBUG
  43. # ifdef LOGFILE
  44. #  undef LOGFILE
  45. # endif
  46. # define LOGFILE "u:/dev/tty"
  47. #endif
  48.  
  49.     if((log_fd = fopen(ux2dos(LOGFILE), "a")) == NULL)
  50.     {    fprintf(stderr,"%s: can't open log file\n",name);
  51.         perror(LOGFILE);
  52.         return;
  53.     }
  54.     if(vfprintf(log_fd,msg,ptr) == EOF)
  55.     {    fprintf(stderr,"%s: can't write to log file\n",name);
  56.         perror(LOGFILE);
  57.         fprintf(stderr,msg);
  58.     }
  59.     fclose(log_fd);
  60.  
  61.     free(msg);
  62.     va_end(ptr);
  63.  
  64. #endif /*LOGFILE */
  65. }
  66.  
  67. /* force a crontabs reread by sending a signal to CROND daemon */
  68.  
  69. void PokeDaemon(char cmd)
  70. {    int pipe;
  71.     
  72.     if((pipe=open(ux2dos(CRONPIPE),O_WRONLY)) >= 0)
  73.     {    if(write(pipe,&cmd,1) == 1)
  74.         {    close(pipe);
  75.             return;
  76.         }
  77.     }
  78.     close(pipe);
  79.     fputs(PROGNAME " : can't send signal to daemon\n",stderr);
  80.     return;
  81. }
  82.  
  83. void AlarmHandler(void)
  84. {    return;
  85. }
  86.  
  87. void cronsleep(unsigned long seconds)
  88. {
  89.     /* Avoid heavy system load while waiting for input :
  90.        Standard sleep functions in PURE C cause enormous load on a
  91.        multitasking system, as they are stupid loops !!
  92.     */
  93.     long mask=sigblock(0L);    /* get current mask */
  94.  
  95.     signal(SIGALRM,AlarmHandler);
  96.  
  97. #ifdef DEBUG
  98.     printf("\nMasked signals: ");
  99.     PrintBin(31,sigblock(0L));
  100.     putchar('\n');
  101. #else
  102.     sigsetmask(mask & ~((1L<<SIGINT)|(1L<<SIGTERM)));
  103.     /* unmask blocked signals */
  104. #endif
  105.  
  106.     alarm(seconds);    /* set alarm time */
  107.     (void)pause();    /* wait for alarm */
  108.  
  109.     sigsetmask(mask);    /* restore old mask when not sleeping */
  110. }
  111.